﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZOIL.Framework.DeviceIO;
using ZOIL.Framework.Presentation;
using Phidgets;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Input;

namespace Library.Devices
{
    public class ZLandscapeDefaultPhidgetHandler : ZLandscapeInputHandler
    {
        InterfaceKit _kit;

        public ZLandscapeDefaultPhidgetHandler(ZInformationLandscape zl)
            : base(zl)
        {
            _kit = new InterfaceKit();
            _kit.InputChange += new Phidgets.Events.InputChangeEventHandler(OnInputChange);
            _kit.SensorChange += new Phidgets.Events.SensorChangeEventHandler(OnSensorChange);
            _kit.Attach += new Phidgets.Events.AttachEventHandler(OnAttach);
            _kit.Detach += new Phidgets.Events.DetachEventHandler(OnDetach);


            _kit.Error += new Phidgets.Events.ErrorEventHandler(OnError);

            _kit.open();
            _kit.waitForAttachment();


        }

        void OnDetach(object sender, Phidgets.Events.DetachEventArgs e)
        {

        }

        private Phidget _phidget;
        private InterfaceKitAnalogSensorCollection _sensors;

        void OnAttach(object sender, Phidgets.Events.AttachEventArgs e)
        {
            _phidget = e.Device;

        }

        void OnError(object sender, Phidgets.Events.ErrorEventArgs e)
        {
            Console.WriteLine(e.Description);
        }

        private int _lastDistance = 1;
        private int _lastX = 0;
        private int _lastY = 0;

        void OnSensorChange(object sender, Phidgets.Events.SensorChangeEventArgs e)
        {
            int index = e.Index;
            int value = e.Value;

            if (index == 1)
            {
                //X

                Point root = new Point(0, 0);
                Point target = new Point(-10, 0);


                if (value < 470)
                {
                    InformationLandscape.Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        Vector v = (root - target);

                        InformationLandscape.PanBy(target - root, false);
                    })

                        , DispatcherPriority.Normal);
                    //pan to the left
                }
                else if (value > 490)
                {
                    InformationLandscape.Dispatcher.BeginInvoke(new Action(delegate()
                    {

                        InformationLandscape.PanBy(root - target, false);
                    })

                        , DispatcherPriority.Normal);
                    //pan to the right
                }



                _lastX = e.Value;
            }
            else if (index == 2)
            {
                //Y
                Point root = new Point(0, 0);
                Point target = new Point(0, -10);


                if (value < 470)
                {
                    InformationLandscape.Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        Vector v = (root - target);

                        InformationLandscape.PanBy(target - root, false);
                    })

                        , DispatcherPriority.Normal);
                    //pan to the left
                }
                else if (value > 490)
                {
                    InformationLandscape.Dispatcher.BeginInvoke(new Action(delegate()
                    {

                        InformationLandscape.PanBy(root - target, false);
                    })

                        , DispatcherPriority.Normal);
                    //pan to the right
                }

                _lastY = e.Value;
            }
            else if (index == 3)
            {
                InformationLandscape.Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        //Point center = new Point(Window.GetWindow(InformationLandscape).Width / 2.0, Window.GetWindow(InformationLandscape).Height / 2.0);
                        //center = InformationLandscape.TransformWindowToLandscapeCoordinates(center);

                        Point center = Mouse.GetPosition(null);

                        if (value > 200)
                            InformationLandscape.ZoomBy(center, 1.1, false);
                        else if (value > 100 && value < 200)
                            InformationLandscape.ZoomBy(center, 0.9, false);
                    })

                        , DispatcherPriority.Normal);
            }
        }

        void OnInputChange(object sender, Phidgets.Events.InputChangeEventArgs e)
        {

        }
    }
}
